package com.sgb.kjwl.view.ui.activity.self.userInfo;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityChangePwdBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.viewmodel.self.userInfo.ChangePwdModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;

import javax.inject.Inject;

public class ChangePwdActivity extends AppActivity {
    private ActivityChangePwdBinding binding;
    @Inject
    ChangePwdModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(ChangePwdActivity.this, R.layout.activity_change_pwd);
        binding.setViewModel(viewModel);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void init() {
        binding.include.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        binding.include.titleView.setText("修改密码");

        binding.tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveValidation();
            }
        });
    }

    private void saveValidation() {
        if (TextUtils.isEmpty(binding.etOldPwd.getText())) {
            MToast.showToast(ChangePwdActivity.this, "旧密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(binding.etNewPwd.getText())) {
            MToast.showToast(ChangePwdActivity.this, "新密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(binding.etReNewPwd.getText())) {
            MToast.showToast(ChangePwdActivity.this, "请再次确认新密码");
            return;
        }

        if (!binding.etNewPwd.getText().toString().equals(binding.etReNewPwd.getText().toString())) {
            MToast.showToast(ChangePwdActivity.this, "两次密码不一致");
            return;
        }

        if (!CheckUtils.isPassword(binding.etNewPwd.getText().toString())) {
            MToast.showToast(ChangePwdActivity.this, R.string.reg_pwd);
            return;
        }

        if (binding.etNewPwd.getText().toString().equals(binding.etOldPwd.getText().toString())) {
            MToast.showToast(ChangePwdActivity.this, "新密码和旧密码不能一致");
            return;
        }

//        viewModel.saveModel(binding.etOldPwd.getText().toString(), binding.etNewPwd.getText().toString());
        viewModel.changePwd(binding.etOldPwd.getText().toString(), binding.etNewPwd.getText().toString());
    }
}
